package com.huayun.plugin.materialLibrary.controller;

import com.huayun.plugin.materialLibrary.domain.MaterialLibrary;
import com.huayun.plugin.materialLibrary.service.MaterialLibraryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/materialLibrary")
public class MaterialLibraryController {

    @Autowired
    private MaterialLibraryService materialLibraryService;

    @PostMapping("/parseExcel")
    public ResponseEntity<List<MaterialLibrary>> uploadFile(@RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            return ResponseEntity.badRequest().body(null);
        }
        try {
            materialLibraryService.parseExcelAndSave(file);
            List<MaterialLibrary> materialLibraryList = materialLibraryService.getMaterialLibraryData();
            return ResponseEntity.ok(materialLibraryList);
        } catch (Exception e) {
            return ResponseEntity.status(500).body(null);
        }
    }
    @GetMapping("/getMaterialLibraryData")
    public ResponseEntity<List<MaterialLibrary>> getMaterialLibraryData() {
        List<MaterialLibrary> materialLibraryList = materialLibraryService.getMaterialLibraryData();
        return ResponseEntity.ok(materialLibraryList);
    }
    @PostMapping("/addMaterialLibraryItem")
    public ResponseEntity<List<MaterialLibrary>> addMaterialLibraryItem(@RequestParam String number,
                                                                        @RequestParam String name,
                                                                        @RequestParam String level,
                                                                        @RequestParam String area,
                                                                        @RequestParam String density,
                                                                        @RequestParam String standard,
                                                                        @RequestParam String unit,
                                                                        @RequestParam Number layer) {
        List<MaterialLibrary> materialLibraryList = materialLibraryService.addMaterialLibraryItem(number,name,level,area,density,standard,unit,layer);
        return ResponseEntity.ok(materialLibraryList);
    }
    @PostMapping("/deleteMaterialLibraryItem")
    public ResponseEntity<List<MaterialLibrary>> deleteMaterialLibraryItem(@RequestParam String id) {
        List<MaterialLibrary> materialLibraryList = materialLibraryService.deleteMaterialLibraryItem(id);
        return ResponseEntity.ok(materialLibraryList);
    }
    @PostMapping("/updateMaterialLibraryItem")
    public ResponseEntity<List<MaterialLibrary>> updateMaterialLibraryItem(@RequestParam String id,
                                                                           @RequestParam String number,
                                                                           @RequestParam String name,
                                                                           @RequestParam String level,
                                                                           @RequestParam String area,
                                                                           @RequestParam String density,
                                                                           @RequestParam String standard,
                                                                           @RequestParam String unit,
                                                                           @RequestParam Number layer) {
        List<MaterialLibrary> materialLibraryList = materialLibraryService.updateMaterialLibraryItem(id,number,name,level,area,density,standard,unit,layer);
        return ResponseEntity.ok(materialLibraryList);
    }
    @GetMapping("/queryMaterialLibraryList")
    public ResponseEntity<List<MaterialLibrary>> queryMaterialLibraryList(@RequestParam String keyWords) {
        List<MaterialLibrary> materialLibraryList = materialLibraryService.queryMaterialLibraryList(keyWords);
        return ResponseEntity.ok(materialLibraryList);
    }
}

